package br.com.erpnasajon.view;

import br.com.erpnasajon.dao.ClienteDao;
import br.com.erpnasajon.modelo.Cliente;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author cgarcia
 */
public class LazyClienteDataModel extends LazyDataModel<Cliente> {
    private ClienteDao dao;  
    private Map<String, String> ultimoFilters;
    
    public LazyClienteDataModel(ClienteDao dao) {  
        this.dao = dao;  
    }  
      
    @Override  
    public Cliente getRowData(String rowKey) {  
        if (dao != null){
            return dao.getCliente(Integer.parseInt(rowKey));
        }
        return null;
    }  
  
    @Override  
    public Object getRowKey(Cliente cliente) {  
        return cliente.getCliente();  
    }
    
    @Override
    public List<Cliente> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
       
        ultimoFilters = filters;
        //Carrega a lista
        List<Cliente> list = dao.load(first, pageSize, sortField, sortOrder, filters);
        
        //Retorna a lista
        return list;
    }
    @Override
    public int getRowCount(){
        int rowCount = dao.rowCount(ultimoFilters);

        return rowCount;
    }
}
